<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="科目">
        <el-select style="width:120px" 
                      v-model="queryParams.subjectId" 
                      placeholder="请选择科目">
                      <el-option
                          v-for="subject in subjectOptions"
                          :key="subject.subjectId"
                          :label="subject.subjectName"
                          :value="subject.subjectId"
                        />
                    </el-select>
                  </el-form-item>
      <el-form-item label="教研组" prop="teacherDeptId">
        <treeselect style="width:220px" v-model="teacherDeptId" :options="teacherDeptOptions" 
                            :multiple="false" :disableBranchNodes="true"
                            :show-count="true" placeholder="请选择教研组" />
                  </el-form-item>
      <el-form-item label="教师">
                    <el-select
                          v-model="queryParams.teachUserId"
                          placeholder="请选择教师"
                          clearable
                          size="small"
                          style="width: 120px"
                        >
                          <el-option
                            v-for="user in teacherUserOptions"
                            :key="user.userId"
                            :label="user.nickName"
                            :value="user.userId"
                          />
                        </el-select>
                  </el-form-item>

      <el-form-item label="时间">
              <el-date-picker
                  v-model="dateRange"
                  size="small"
                  style="width: 240px"
                  value-format="yyyy-MM-dd"
                  type="daterange"
                  range-separator="-"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['edu:classJournal:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['edu:classJournal:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['edu:classJournal:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['edu:classJournal:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="classJournalList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="教学日志ID" align="center" prop="journalId" />-->
      <el-table-column label="班级" align="center" prop="className" width="150" />
      <el-table-column label="教师" align="center" prop="teachUserName" />
      <el-table-column label="科目" align="center" prop="subjectName" />
      <el-table-column label="课次" align="center" prop="classTimes" />
      <el-table-column label="自评结果" :formatter="selfCommentsFlagFormat" align="center" prop="selfCommentsFlag" />
      <el-table-column label="教学效果" :formatter="teachEffectFlagFormat" align="center" prop="teachEffectFlag" width="180" />
      <el-table-column label="课堂纪律" :formatter="classDisciplineFlagFormat" align="center" prop="classDisciplineFlag" />
      <!--<el-table-column label="教师备注" align="center" prop="selfNotes" />
      <el-table-column label="状态" :formatter="statusFormat" align="center" prop="status" />-->
      <el-table-column label="完成状态" :formatter="processStatusFormat" align="center" prop="processStatus" />
      <el-table-column label="操作" fixed="right" min-width="180px" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['edu:classJournal:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="examineJournal(scope.row)"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['edu:classJournal:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改教学日志对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="教研组" prop="teacherDeptId">
        <treeselect v-model="teacherDeptIdForm" :options="teacherDeptOptions" 
                            :multiple="false" :disableBranchNodes="true"
                            :show-count="true" placeholder="请选择教研组" />
                  </el-form-item>
      <el-form-item label="教师">
                    <el-select
                          v-model="form.teachUserId"
                          placeholder="请选择教师"
                          clearable
                          size="small"
                          style="width: 120px"
                        >
                          <el-option
                            v-for="user in teacherUserOptionsForm"
                            :key="user.userId"
                            :label="user.nickName"
                            :value="user.userId"
                          />
                        </el-select>
                  </el-form-item>

        <el-form-item label="课表" prop="scheduleInfoId">
          <el-select v-model="form.scheduleInfoId" placeholder="请输入课表详情" clearable size="small">
            <el-option
                  v-for="item in scheduleInfoOptions"
                  :key="item.scheduleInfoId"
                  :label="item.dictLabel"
                  :value="item.scheduleInfoId"
                >
                <span style="float: left">{{ item.subjectName }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ '第 ' + item.courseTimes + ' 次课'}}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="自评结果" prop="selfCommentsFlag">
          <el-select v-model="form.selfCommentsFlag" placeholder="请选择自评结果" clearable size="small">
            <el-option
                  v-for="dict in selfCommentsFlagOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
          </el-select>
        </el-form-item>
        <el-form-item label="教学效果" prop="teachEffectFlag">
          <el-select v-model="form.teachEffectFlag" placeholder="请选择教学效果" clearable size="small">
            <el-option
                  v-for="dict in teachEffectFlagOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
          </el-select>
        </el-form-item>
        <el-form-item label="课堂纪律" prop="classDisciplineFlag">
          <el-select v-model="form.classDisciplineFlag" placeholder="请选择课堂纪律" clearable size="small">
            <el-option
                  v-for="dict in classDisciplineFlagOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
          </el-select>
        </el-form-item>
        <el-form-item label="教师备注" prop="selfNotes">
          <el-input v-model="form.selfNotes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
                        <el-radio
                          v-for="dict in statusOptions"
                          :key="dict.dictValue"
                          :label="dict.dictValue"
                        >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="mini" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 审核对话框 -->
    <el-dialog :title="title" :visible.sync="examineOpen" width="800px" append-to-body>
      <el-form ref="examineParams" :model="examineForm" :rules="rules" label-width="120px">
        <el-form-item label="审核状态">
          <el-radio-group v-model="examineParams.status">
                        <el-radio
                          v-for="dict in processStatusOptions"
                          :key="dict.dictValue"
                          :label="dict.dictValue"
                        >{{dict.dictLabel}}</el-radio>
            </el-radio-group>
        </el-form-item>

        <el-form-item label="审核内容" prop="selfNotes">
          <el-input v-model="examineParams.examineNotes" type="textarea" placeholder="请输入内容" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="submitExamine">确 定</el-button>
        <el-button size="mini" @click="examineCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listClassJournal, getClassJournal, delClassJournal, addClassJournal, updateClassJournal, exportClassJournal, examineNotice } from "@/api/edu/classJournal";
import { treeselectByType } from "@/api/system/dept";
import { getSubjectsAll } from "@/api/edu/subjects";
import { getListByTechUserId } from "@/api/edu/scheduleInfo";
import { listAllUserByDeptId } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "ClassJournal",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教学日志表格数据
      classJournalList: [],
      teacherDeptId: null,
      teacherDeptIdForm: null,
      subjectOptions: [],
      teacherDeptOptions: [],
      teacherDeptOptionsForm: [],
      teacherUserOptions: [],
      selfCommentsFlagOptions: [],
      teachEffectFlagOptions: [],
      classDisciplineFlagOptions: [],
      scheduleInfoOptions: [],
      statusOptions: [],
      processStatusOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示审核弹出层
      examineOpen: false,
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        subjectId: null,
        teachUserId: null,
        scheduleInfoId: null,
        selfCommentsFlag: null,
        teachEffectFlag: null,
        classDisciplineFlag: null,
        selfNotes: null,
        status: null,
      },
      // 审核参数
      examineParams: {
        status: null,
        examineNotes: null
      },
      // 表单参数
      form: {},
      // 审核表单参数
      examineForm: {
        journalId: null
      },
      // 表单校验
      rules: {
        scheduleInfoId: [
          { required: true, message: "课表详情ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getSubjects()
    this.queryTeacherDept()
    this.getDicts("edu_self_comments_flag").then(response => {
      this.selfCommentsFlagOptions = response.data;
    });
    this.getDicts("edu_teach_effect_flag").then(response => {
      this.teachEffectFlagOptions = response.data;
    });
    this.getDicts("edu_class_discipline_flag").then(response => {
      this.classDisciplineFlagOptions = response.data;
    });
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("edu_process_status").then(response => {
      this.processStatusOptions = response.data;
    });
    
    this.getList();
  },
  watch: {
    'teacherDeptId': 'selectQueryTeacher',
    'teacherDeptIdForm': 'selectQueryTeacherForm',
    'form.teachUserId': 'selectQueryScheduleInfo'
  },
  methods: {
    getSubjects() {
      getSubjectsAll().then(response => {
        this.subjectOptions = response.data
      })
    },
    queryTeacherDept() {
      // 查询教学组（deptType = 4）
      treeselectByType("4").then(response => {
        this.teacherDeptOptions = response.data;
      })
    },
    selfCommentsFlagFormat(row, column) {
      return this.selectDictLabel(this.selfCommentsFlagOptions, row.selfCommentsFlag);
    },
    teachEffectFlagFormat(row, column) {
      return this.selectDictLabel(this.teachEffectFlagOptions, row.teachEffectFlag);
    },
    classDisciplineFlagFormat(row, column) {
      return this.selectDictLabel(this.classDisciplineFlagOptions, row.classDisciplineFlag);
    },
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    processStatusFormat(row, column) {
      return this.selectDictLabel(this.processStatusOptions, row.processStatus);
    },
    selectQueryTeacher() {
      if(this.teacherDeptId === undefined || this.teacherDeptId === null) {
        this.$message({
            type: 'warning',
            message: '请选择教学组'
          })
          return
      }
      listAllUserByDeptId(this.teacherDeptId).then(response => {
        this.teacherUserOptions = response.data
      })
    },
    selectQueryTeacherForm() {
      if(this.teacherDeptIdForm === undefined || this.teacherDeptIdForm === null) {
        this.$message({
            type: 'warning',
            message: '请选择教学组'
          })
          return
      }
      listAllUserByDeptId(this.teacherDeptIdForm).then(response => {
        this.teacherUserOptionsForm = response.data
      })
    },
    selectQueryScheduleInfo() {
      if(this.form.teachUserId === undefined || this.form.teachUserId === null) {
        this.$message({
            type: 'warning',
            message: '请选择教师'
          })
          return
      }

      getListByTechUserId(this.form.teachUserId).then(response => {
        this.scheduleInfoOptions = response.data
      })
    },
    /** 查询教学日志列表 */
    getList() {
      this.loading = true;
      listClassJournal(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.classJournalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        journalId: null,
        scheduleInfoId: null,
        teachUserId: null,
        classId: null,
        subjectId: null,
        teachUserName: null,
        className: null,
        subjectName: null,
        classTimes: null,
        selfCommentsFlag: null,
        teachEffectFlag: null,
        classDisciplineFlag: null,
        selfNotes: null,
        status: "0",
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.resetForm("examineForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.journalId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加教学日志";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const journalId = row.journalId || this.ids
      getClassJournal(journalId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改教学日志";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.journalId != null) {
            updateClassJournal(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClassJournal(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const journalIds = row.journalId || this.ids;
      this.$confirm('是否确认删除教学日志编号为"' + journalIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delClassJournal(journalIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有教学日志数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportClassJournal(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    resetExamineForm() {
      this.examineParams.status = null;
      this.examineParams.examineNotes = null;
    },
    /** 审核按钮操作 */
    examineJournal(row) {
      this.resetExamineForm();
      const journalId = row.journalId || this.ids;
      this.examineForm.journalId = journalId;
      this.examineOpen = true;
      this.title = "审核日志";

    },
    /** 审核确定操作*/
    submitExamine() {
      const journalId = this.examineForm.journalId;
      const status = this.examineParams.status;
      const examineNotes = this.examineParams.examineNotes;
      this.$confirm('是否确认审核教师日志编号为"' + journalId + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return examineNotice(journalId, status, examineNotes);
        }).then(() => {
          this.msgSuccess("审核成功");
          this.examineOpen = false;
          this.getList();
        })
    },
    /** 审核取消操作*/
    examineCancel() {
      this.examineOpen = false;
      this.resetExamineForm();
    },
  }
};
</script>
